#ifndef  FOC_H
#define  FOC_H

#include "type_define.h"


/* Start Parameter */


#define TestPINP35			GPIO3->DO_f.P5
#define SetTestPIN			TestPINP35 = 1
#define ClrTestPIN			TestPINP35 = 0
#define TroggleTestPIN	TestPINP35 ^= 1;

#define ADD_STEP_VAL			10

#define _4_0DEG 		720
#define _2_0DEG 		360
#define _1_0DEG 		182
#define _0_5DEG 		91
#define _0_4DEG 		72
#define _0_2DEG 		36
#define _0_1DEG 		18// The value for 0.05 degrees is converted
#define _0_05DEG 		9	// The value for 0.05 degrees is converted
#define _0_03DEG 		6	// The value for 0.05 degrees is converted
#define _0_025DEG 	4	// The value for 0.05 degrees is converted




typedef struct 
{
	u8 SVPWMMode;
	u8 Sector;
	u8 LastSector;
	u8 SectorAngle;
	
	s16 Cos;
	s16 Sin;
	
	s16 PWMPeriod;
	s32 Vd;
	s32 Vq;
	s32 Valpha;
	s32 Vbeta;
//	s16 Tu;
//	s16 Tv;
//	s16 Tw;
	
	s16 TuUp;
	s16 TvUp;
	s16 TwUp;
	s16 TuDn;
	s16 TvDn;
	s16 TwDn;
	s16 T1;
	s16 T2;
	s16 Pointer;

	s16 Angle;
	s16 LastAngle;
	s16 HeadAngle;
	s16 VoltAngle;
	s16 DeltaVoltAngle;
	s16 StepVoltAngle;
	s32 IuIn;
	s32 IvIn;
	s32 IwIn;
	s32 Ialpha;
	s32 Ibeta;
	s32 Id;
	s32 IdRef;
	s32 Iq;
	s32 TargetIq;
	s32 IqFilter;
	s32 IqPress;
	s32 IqRef;
	s32 VsVolts;
	s32 BusVolts;
	s32 BusCurrent;
	s32 SvpwmVolts;
	s32 VdLimit;
	s32 VqLimit;
//	s32 BusCurrentLevel;
	s32 PressBusCurrentLevel;
	s32 PourBusCurrentLevel;
	s16 DTCU;
	s16 DTCV;
	s16 DTCW;
	s16 VqCalCoeff;
	s32 ClosedPressVq;
	s32 ClosedTargetVq;
	s32 MinVq;
	
	s16 ClosedNowIq;
	s16 ClosedTargetIq;
	
	s32 PowerValue;
	s32 Vref1;
	s32 Vref2;
	s32 Vref3;
	u16 ChargeCnt; 
	u16 BrakeCnt; 
 }foc_struct;

extern volatile foc_struct Foc_lib;
extern volatile u8 AddStep;
extern volatile s16 AngleCal;






typedef enum 
{
	MC_INIT		= 0, 
	MC_STOP		= 1,
	MC_CHARGE	= 2, 
	MC_SQUARE	= 3,
	MC_FOC	= 4,
	MC_WINDMILL	= 5, 
	MC_BREAK = 6,
	MC_RESTART = 7,
	MC_TORQUE_HOLD = 7,
}MC_MotorStates;
extern volatile MC_MotorStates MOTOR_STATE;
extern volatile MC_MotorStates TARGET_STATE;



extern const s16 sincostab16[];



void FOCTestFun(void);
void ReadyStopFun(void);
void BrakeStateFun(void);
void MotorRunFun(void);
void MotorFocRunFun(void);
void MotorSquareRunFun(void);
void ResetOtherParameterFun(void);
void AddVqControlFun(void);

#endif       

